package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author Mylinja 2021-12-21 星期二 17:18
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    /**
     * 添加购物车方法
     *
     * @param shoppingCart 用户传递的菜品或套餐信息
     * @return 返回添加到购物车的对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShoppingCart addCart(ShoppingCart shoppingCart) {
        // 设置用户id
        Long userid = BaseContext.getCurrentId();
        shoppingCart.setUserId(userid);

        // 创建查询器
        LambdaQueryWrapper<ShoppingCart> query = new LambdaQueryWrapper<>();
        query.eq(ShoppingCart::getUserId, userid);

        // 每次只会添加一个条目到购物车里面去
        if (shoppingCart.getDishId() != null) {
            // 证明添加到购物车里面的是菜品
            query.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            // 证明添加到购物车里面的是套餐
            query.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        // 判断当前添加的条目在购物车中是否存在
        //SQL: select * from shoppingCart where user_id = ? and dish_id/setmeal_id = ?
        ShoppingCart cartServiceOne = this.getOne(query);
        if (cartServiceOne != null) {
            // 已经存在该菜品或者套餐，那么在之前的基础上+1
            cartServiceOne.setNumber(cartServiceOne.getNumber() + 1);
            // 注意 这里添加完成后，需要更新数据库
            this.updateById(cartServiceOne);
        } else {
            // 不存在该菜品，那么把该菜品添加到shoppingCart表里面
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            this.save(shoppingCart);
            cartServiceOne = shoppingCart;
        }

        return cartServiceOne;
    }

    /**
     * 删除一条购物车里面的条目
     * @param shoppingCart 用来封装setmealId
     * @return 返回购物车对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ShoppingCart deleteInShoppingCart(ShoppingCart shoppingCart) {
        // 前端传递过来的是setmealId
        // SQL: select * from shoppingCart where setmeal_id/dish_id = ? and user_id = ?
        Long setmealId = shoppingCart.getSetmealId();
        Long dishId = shoppingCart.getDishId();

        long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> query = new LambdaQueryWrapper<>();
        query.eq(ShoppingCart::getUserId, userId);

        if (dishId != null) {
            query.eq(ShoppingCart::getDishId, dishId);
        }else {
            query.eq(ShoppingCart::getSetmealId, setmealId);
        }

        ShoppingCart one = this.getOne(query);

        Integer number = one.getNumber();
        if (number - 1 <= 0) {
            // 购物车里面的这个条目，查询出来只有一条，那么直接删除这条数据
            this.removeById(one.getId());
        } else {
            one.setNumber(number - 1);
            this.updateById(one);
        }
        return one;
    }
}
